//
//  UPSXunJianViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-10.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "UPSXunJianViewController.h"

@interface UPSXunJianViewController ()
{
    NSMutableArray *checkNameLBBS;
    NSString *checkResult;
}
@end

@implementation UPSXunJianViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"UPS监视主机每周巡检";
    [super viewDidLoad];
    self.sendUrl = addUPSData;
    [Appinfomation shareIstance].strongXunJianCount = 191;
    self.myTableView.frame = CGRectMake(interval111/2, 140, self.myTableView.frame.size.width, self.myTableView.frame.size.height - (140-64));
    self.whiteImageView.frame = CGRectMake(self.myTableView.frame.size.width + self.myTableView.frame.origin.x, self.myTableView.frame.origin.y, self.whiteImageView.frame.size.width, self.whiteImageView.frame.size.height - (140-64));
    self.backScrollV.frame = CGRectMake(10, 10, self.whiteImageView.frame.size.width - 20, self.whiteImageView.frame.size.height - 20);
    
    for (int i = 0; i<3; i ++)
    {
        UILabel *headTitleLb = [UILabel new];
        switch (i)
        {
            case 0:
                headTitleLb.text = @"主机状况：";
                break;
            case 1:
                headTitleLb.text = @"软件状况：";
                break;
            case 2:
                headTitleLb.text = @"通讯状况：";
                break;
                
            default:
                break;
        }
        headTitleLb.textColor = [UIColor whiteColor];
        headTitleLb.font = [UIFont systemFontOfSize:30];
        CGSize mySz = [headTitleLb.text sizeWithFont:headTitleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        headTitleLb.frame = CGRectMake(interval111 + (scrennWidth - interval111*2)/3 * i, 80, mySz.width,mySz.height);
        [self.view addSubview:headTitleLb];
        
        UITextField *TF = [[UITextField alloc]initWithFrame:CGRectMake(headTitleLb.frame.origin.x + headTitleLb.frame.size.width, headTitleLb.frame.origin.y, 150, mySz.height)];
        TF.background = [UIImage imageNamed:@"输入框底"];
        [self.view addSubview:TF];
        TF.delegate = self;
        TF.tag = 100+i;
    }
//    NSURL *queryUrl = [NSURL URLWithString:queryUPSFloor];
//    obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
//    self.sendUrl=[NSString stringWithFormat:addUPSData];
//    [obtainFloor setPostValue:@"" forKey:@""];
//    [obtainFloor startSynchronous];
//    
//    NSString *response = [obtainFloor responseString];
//    NSDictionary *queryDic = [response JSONValue];
    NSArray *queryDataArr = @[@{@"id": @"1",@"floorname":@"4FB1",@"rfid":@"<#string#>"},@{@"id": @"2",@"floorname":@"4FB2",@"rfid":@"<#string#>"},@{@"id": @"3",@"floorname":@"4FB3",@"rfid":@"<#string#>"},@{@"id": @"4",@"floorname":@"4FA",@"rfid":@"<#string#>"},@{@"id": @"5",@"floorname":@"3FB(a路)",@"rfid":@"<#string#>"},@{@"id": @"6",@"floorname":@"3FB(b路)",@"rfid":@"<#string#>"},@{@"id": @"7",@"floorname":@"B1F",@"rfid":@"<#string#>"},@{@"id": @"8",@"floorname":@"1F",@"rfid":@"<#string#>"},@{@"id": @"9",@"floorname":@"9F",@"rfid":@"<#string#>"},@{@"id": @"10",@"floorname":@"27F",@"rfid":@"<#string#>"},@{@"id": @"11",@"floorname":@"28F",@"rfid":@"<#string#>"},@{@"id": @"12",@"floorname":@"5FUPS",@"rfid":@"<#string#>"}];
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorname"]];
        [self.floorIds addObject:[dic valueForKey:@"id"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"id"] }];
    }
    
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    [self.view bringSubviewToFront:self.blackGroundV];
    [self.view bringSubviewToFront:self.blackStopV];
}

- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    checkNameLBBS = [NSMutableArray array];
    self.floorIdQuanju = floorId;

    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObject:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        checkResult = [[receiveFloorObj objectForKey:@"result"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    else
    {
        checkResult = @"";
    }

    UIImage *textImage = [UIImage imageNamed:@"输入框底"];
    UILabel *checkNameLb = [UILabel new];
    checkNameLb.text = @"备电时间(m)：";
    checkNameLb.font = [UIFont systemFontOfSize:30];
    CGSize chSize = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    checkNameLb.frame = CGRectMake(10, [[checkNameLBBS lastObject]frame].origin.y + [[checkNameLBBS lastObject]frame].size.height + 10, chSize.width, chSize.height);
    [self.backScrollV addSubview:checkNameLb];
    [checkNameLBBS addObject:checkNameLb];
    [self.controlWillClears addObject:checkNameLb];
    
    UITextField *checkTf = [[UITextField alloc]initWithFrame:CGRectMake(200, checkNameLb.frame.origin.y, 340, chSize.height)];
    checkTf.delegate = self;
    checkTf.keyboardType = UIKeyboardTypeNumberPad;
    checkTf.text = checkResult;
    checkTf.background = textImage;
    [self.backScrollV addSubview:checkTf];
    [self.controlWillClears addObject:checkTf];
    
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        [self addCommentKuang:[receiveFloorObj objectForKey:@"floorRemark"]];
    }
    else
    {
        [self addCommentKuang:@""];
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == self.myTableView) {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 100:
        {
            self.hostStr = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
            break;
        case 101:
        {
            self.softwareStr = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
            break;
        case 102:
        {
            self.communicationStr = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
            break;
            
        default:
        {
            checkResult = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
            break;
    }
}
- (void)jajajajaj:(UIButton*)btn
{
    UIImage *submitImage = [UIImage imageNamed:@"提交"];
    switch (btn.tag)
    {
        case 0:
        {
            int Count = 0;
            NSDictionary *dicx = @{@"floorId": self.floorIdQuanju,@"floorRemark": [self.commentTv.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],@"result": checkResult};
            for (int i = 0; i < self.sendFloorDatas.count; i++)
            {
                id str = [self.sendFloorDatas objectAtIndex:i];
                if ([str isKindOfClass:[NSString class]])
                {
                    Count ++;
                }
            }
            if (Count > 0 )
            {
                [self.sendFloorDatas replaceObjectAtIndex:[self.floorIds indexOfObjectss: self.floorIdQuanju] withObject:dicx];
                UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"数据保存成功。。。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [myAlert show];
                [self gotoNextFloor];
                if (Count == 1)
                {
                    [btn setImage:submitImage forState:UIControlStateNormal];
                }
            }
            else
            {
                
                UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请确认您的用户身份"  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alertV.tag = 250;
                [alertV setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
                [alertV textFieldAtIndex:0].text = [[Appinfomation shareIstance].loginDic valueForKey:@"user"];
                [alertV show];
            }
        }
            
            break;
        case 1:
        {
            self.navigationController.navigationBar.hidden = YES;
            self.blackGroundV.hidden = NO;
        }
            break;
        case 2:
        {
            self.navigationController.navigationBar.hidden = YES;
            self.blackStopV.hidden = NO;
        }
            break;
            
        default:
            break;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [super alertView:alertView clickedButtonAtIndex:buttonIndex];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
